package fu.capstone.hanoicityguide.presentation.fragment.itinerary;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.facebook.UiLifecycleHelper;

import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.bo.itinerary.ItineraryBO;
import fu.capstone.hanoicityguide.business.bo.itinerary.ItineraryBOImp;
import fu.capstone.hanoicityguide.business.model.Place;
import fu.capstone.hanoicityguide.common.utility.InternetSetting;
import fu.capstone.hanoicityguide.common.utility.SharingFacebook;
import fu.capstone.hanoicityguide.presentation.adapter.ListItineraryDetailAdapter;
import fu.capstone.hanoicityguide.presentation.adapter.ViewPagerAdapter;
import fu.capstone.hanoicityguide.presentation.fragment.attraction.DetailAttractionFragment;
import fu.capstone.hanoicityguide.presentation.fragment.home.HomeFragment;
import fu.capstone.hanoicityguide.presentation.fragment.hotel.DetailHotelFragment;
import fu.capstone.hanoicityguide.presentation.fragment.nightlife.DetailNightLifeFragment;
import fu.capstone.hanoicityguide.presentation.fragment.restaurant.DetailRestaurantFragment;

@SuppressLint("ValidFragment")
public class DetailItineraryFragment extends Fragment {

	private ItineraryBO itineraryBO;
	private int itineraryID;
	private ViewPager imgFragment;
	private ViewPagerAdapter vpagerAdapter;
	private ListView itineraryListView = null;
	private String title;
	private ListItineraryDetailAdapter adapter = null;
	private ArrayList<Place> arrPlace;
	private UiLifecycleHelper uiHelper;
	private InternetSetting internetSetting;

	public DetailItineraryFragment() {

	}

	public DetailItineraryFragment(int itineraryID, String title) {
		this.itineraryID = itineraryID;
		this.title = title;
	}

	public int getItineraryID() {
		return this.itineraryID;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View browseItineraryFragment = inflater.inflate(
				R.layout.fragment_detail_itinerary, container, false);
		try {
			setHasOptionsMenu(true);
			internetSetting = new InternetSetting();
			itineraryBO = new ItineraryBOImp(container.getContext());

			// get data from database
			arrPlace = new ArrayList<Place>();
			arrPlace = itineraryBO.browseItinerary(getItineraryID());

			// fill data into components
			itineraryListView = (ListView) browseItineraryFragment
					.findViewById(R.id.listItineraryDetail);

			adapter = new ListItineraryDetailAdapter(getActivity(),
					R.layout.adapter_list_itinerarydetail, arrPlace);

			itineraryListView.setAdapter(adapter);

			itineraryListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// call Detail Fragment of Place
					Fragment browsePlace = null;
					switch (arrPlace.get(position).getCategory()) {
					case "Attraction":
						browsePlace = new DetailAttractionFragment(arrPlace
								.get(position).getId());
						break;
					case "Hotel":
						browsePlace = new DetailHotelFragment(arrPlace.get(
								position).getId());
						break;
					case "Restaurant":
						browsePlace = new DetailRestaurantFragment(arrPlace
								.get(position).getId());
						break;
					case "Nightlife":
						browsePlace = new DetailNightLifeFragment(arrPlace.get(
								position).getId());
						break;
					default:
						break;
					}

					if (browsePlace != null) {
						FragmentManager fragmentManager = getFragmentManager();
						FragmentTransaction fTransaction = fragmentManager
								.beginTransaction();
						fTransaction.addToBackStack(null);
						fTransaction.replace(R.id.frame_container, browsePlace)
								.commit();
						getActivity()
								.setTitle(arrPlace.get(position).getName());
					}
				}
			});

			imgFragment = (ViewPager) browseItineraryFragment
					.findViewById(R.id.itiPager);

			ArrayList<String> arrImage = new ArrayList<String>();
			for (int i = 0; i < arrPlace.size(); i++) {
				for (int j = 0; j < arrPlace.get(i).getArrImage().size(); j++) {
					arrImage.add(arrPlace.get(i).getArrImage().get(j));
				}
			}

			vpagerAdapter = new ViewPagerAdapter(getActivity(), arrImage);

			imgFragment.setAdapter(vpagerAdapter);
		} catch (Exception e) {
			Log.e("DetailItineraryFragment", e.toString());
		}

		return browseItineraryFragment;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		inflater = getActivity().getMenuInflater();
		inflater.inflate(R.menu.detailed_place_page_menu, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Fragment fragment = new Fragment();
		internetSetting = new InternetSetting(getActivity());
		switch (item.getItemId()) {
		case R.id.action_map: {

			if (internetSetting.getConnectivityStatus(getActivity())) {
				fragment = new ItineraryOnlineMapFragment();
				Bundle bundle = new Bundle();
				bundle.putInt("ItineraryID", itineraryID);
				fragment.setArguments(bundle);
				if (fragment != null) {
					FragmentManager fragmentManager = getFragmentManager();
					FragmentTransaction fTransaction = fragmentManager
							.beginTransaction();
					fTransaction.addToBackStack(null);
					fTransaction.replace(R.id.frame_container, fragment)
							.commit();
					getActivity().setTitle("View on map");
				}
			}

			if (!internetSetting.getConnectivityStatus(getActivity())
					&& HomeFragment.isPurchased) {
				fragment = new ItineraryOfflineMapFragment();
				Bundle bundle = new Bundle();
				bundle.putInt("ItineraryID", itineraryID);
				fragment.setArguments(bundle);
				if (fragment != null) {
					FragmentManager fragmentManager = getFragmentManager();
					FragmentTransaction fTransaction = fragmentManager
							.beginTransaction();
					fTransaction.addToBackStack(null);
					fTransaction.replace(R.id.frame_container, fragment)
							.commit();
					getActivity().setTitle("View on map");
				}
			}

			if (!internetSetting.getConnectivityStatus(getActivity())
					&& !HomeFragment.isPurchased) {
				internetSetting.showSettingsAlert();
			}
		}
			break;
		case R.id.action_checkin: {
			if (internetSetting.getConnectivityStatus(getActivity())) {
				uiHelper = new UiLifecycleHelper(getActivity(), null);
				SharingFacebook.shareFacebook(uiHelper, getActivity());
			} else {
				internetSetting.showSettingsAlert();
			}
		}
		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getActivity().getActionBar().setTitle(getTitle());
	}
}
